package gui;

import gameObjects.Castle;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import upgrades.Upgrade;
import castlewarserver.Connector;

public class CastleMenu extends JFrame {

	private static final long serialVersionUID = 1L;

	Castle castle;
	Connector connector;
	String player;
	MainWindow mainWindow;

	JLabel infantryLabel;
	JLabel infantryPriceLabel;

	JLabel pikemanLabel;
	JLabel pikemanPriceLabel;

	JLabel cavalryLabel;
	JLabel cavalryPriceLabel;
	MouseListener mouseListener;

	JLabel goldLabel;
	JScrollPane upgradesScrollPane;
	MainPanel upgradesPanel;
	private JLabel lblCastleHp;
	JLabel castleHpLabel;
	JButton okButton;
	private JLabel lblPrice;
	private JLabel label_1;
	private JLabel lblName;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;

	public CastleMenu( Connector connector,String player,MainWindow mainWindow,Castle castle) {
		setTitle("Castle Menu");
		getContentPane().setForeground(new Color(255, 215, 0));
		getContentPane().setFont(new Font("Tahoma", Font.BOLD, 11));

		this.connector=connector;
		this.player= player;
		this.mainWindow=mainWindow;
		this.castle=castle;
		init();
	}

	public void update(){
		String color;

		if( player.equalsIgnoreCase("ORANGE") ){
			castle= connector.getCastle("ORANGE");
			color="orange";
		}
		else{
			castle= connector.getCastle("BLUE");
			color="blue";
		}
		String path= "units\\"+color+"\\";


		infantryLabel.setIcon(new ImageIcon(path+"infantry.png"));		
		pikemanLabel.setIcon(new ImageIcon(path+"pikeman.png"));
		cavalryLabel.setIcon(new ImageIcon(path+"cavalry.png"));

		infantryPriceLabel.setText(String.valueOf(castle.getInfantryPrice()));
		pikemanPriceLabel.setText(String.valueOf(castle.getPikemanPrice()));
		cavalryPriceLabel.setText(String.valueOf(castle.getCavalryPrice()));

		goldLabel.setText(String.valueOf(castle.getGold()));
		mainWindow.goldLabel.setText(String.valueOf(castle.getGold()));

		castleHpLabel.setText(String.valueOf(castle.getHp()));
		mainWindow.castleHpLabel.setText(String.valueOf(castle.getHp()));

		drawUpgrades();

		getContentPane().repaint();

	}

	private void init(){
		getContentPane().setLayout(null);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(10, 10, 288, 468);
		setMaximumSize(new Dimension(288, 468));
		setMinimumSize(new Dimension(288, 468));
		setResizable(false);

		JLayeredPane layeredPane = new JLayeredPane();
		layeredPane.setBounds(0, 0, 283, 440);
		getContentPane().add(layeredPane);

		JLabel lblBuyInfantry = new JLabel("Buy infantry");
		lblBuyInfantry.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblBuyInfantry.setForeground(new Color(255, 215, 0));
		lblBuyInfantry.setBounds(10, 63, 249, 14);
		layeredPane.add(lblBuyInfantry);

		infantryLabel = new JLabel(new ImageIcon("\\units\\blue\\infantry.png"));
		infantryLabel.setBounds(110, 54, 32, 32);
		layeredPane.add(infantryLabel);

		infantryPriceLabel = new JLabel("0");
		infantryPriceLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
		infantryPriceLabel.setForeground(new Color(255, 215, 0));
		infantryPriceLabel.setBounds(207, 63, 76, 14);
		layeredPane.add(infantryPriceLabel);

		JLabel lblBuyPikeman = new JLabel("Buy pikeman");
		lblBuyPikeman.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblBuyPikeman.setForeground(new Color(255, 215, 0));
		lblBuyPikeman.setBounds(10, 106, 249, 14);
		layeredPane.add(lblBuyPikeman);

		pikemanLabel = new JLabel(new ImageIcon("\\units\\blue\\pikeman.png"));
		pikemanLabel.setBounds(110, 97, 32, 32);
		layeredPane.add(pikemanLabel);

		pikemanPriceLabel = new JLabel("0");
		pikemanPriceLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
		pikemanPriceLabel.setForeground(new Color(255, 215, 0));
		pikemanPriceLabel.setBounds(207, 106, 76, 14);
		layeredPane.add(pikemanPriceLabel);

		JLabel lblBuyCavalary = new JLabel("Buy cavalary");
		lblBuyCavalary.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblBuyCavalary.setForeground(new Color(255, 215, 0));
		lblBuyCavalary.setBounds(10, 149, 249, 14);
		layeredPane.add(lblBuyCavalary);

		cavalryLabel = new JLabel(new ImageIcon("\\units\\blue\\cavalary.png"));
		cavalryLabel.setBounds(110, 140, 32, 32);
		layeredPane.add(cavalryLabel);

		cavalryPriceLabel = new JLabel("0");
		cavalryPriceLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
		cavalryPriceLabel.setForeground(new Color(255, 215, 0));
		cavalryPriceLabel.setBounds(207, 149, 76, 14);
		layeredPane.add(cavalryPriceLabel);

		JLabel lblYourGold = new JLabel("Your gold:");
		lblYourGold.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblYourGold.setForeground(new Color(255, 215, 0));
		lblYourGold.setBounds(24, 11, 66, 14);
		layeredPane.add(lblYourGold);

		goldLabel = new JLabel("0");
		goldLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
		goldLabel.setForeground(new Color(255, 215, 0));
		goldLabel.setBounds(91, 11, 51, 14);
		layeredPane.add(goldLabel);

		JLabel lblUpgrades = new JLabel("Upgrades");
		lblUpgrades.setHorizontalAlignment(SwingConstants.CENTER);
		lblUpgrades.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblUpgrades.setForeground(new Color(255, 215, 0));
		lblUpgrades.setBounds(10, 183, 273, 14);
		layeredPane.add(lblUpgrades);

		upgradesPanel = new MainPanel();
		upgradesPanel.setOpaque(false);
		upgradesPanel.setPreferredSize(new Dimension(250, 200));
		upgradesScrollPane = new JScrollPane(upgradesPanel);
		upgradesScrollPane.setBorder(new LineBorder(new Color(255, 215, 0), 2, true));
		upgradesScrollPane.setBounds(10, 226, 253, 125);
		upgradesScrollPane.setLayout(new ScrollPaneLayout());
		upgradesScrollPane.getViewport().setOpaque(false);
		upgradesScrollPane.setOpaque(false);
		upgradesScrollPane.getVerticalScrollBar().setUI( new CustomScrollBarUI());
		upgradesScrollPane.getHorizontalScrollBar().setUI( new CustomScrollBarUI());
		layeredPane.add(upgradesScrollPane);

		lblCastleHp = new JLabel("Castle HP:");
		lblCastleHp.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblCastleHp.setForeground(new Color(255, 215, 0));
		lblCastleHp.setBounds(152, 11, 66, 14);
		layeredPane.add(lblCastleHp);

		castleHpLabel = new JLabel("0");
		castleHpLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
		castleHpLabel.setForeground(new Color(255, 215, 0));
		castleHpLabel.setBounds(222, 11, 51, 14);
		layeredPane.add(castleHpLabel);

		okButton = new JButton(new ImageIcon("images\\buttonOk.png"));
		okButton.setBounds(93, 379, 100, 50);
		okButton.setOpaque(false);
		layeredPane.add(okButton);

		lblPrice = new JLabel("Price");
		lblPrice.setForeground(new Color(255, 215, 0));
		lblPrice.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblPrice.setHorizontalAlignment(SwingConstants.CENTER);
		lblPrice.setBounds(190, 38, 46, 14);
		layeredPane.add(lblPrice);

		label_1 = new JLabel("Price");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setForeground(new Color(255, 215, 0));
		label_1.setFont(new Font("Vijaya", Font.BOLD, 17));
		label_1.setBounds(190, 201, 46, 14);
		layeredPane.add(label_1);

		lblName = new JLabel("Name");
		lblName.setFont(new Font("Vijaya", Font.BOLD, 17));
		lblName.setForeground(new Color(255, 215, 0));
		lblName.setHorizontalAlignment(SwingConstants.CENTER);
		lblName.setBounds(10, 38, 76, 14);
		layeredPane.add(lblName);

		label_2 = new JLabel("Name");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setForeground(new Color(255, 215, 0));
		label_2.setFont(new Font("Vijaya", Font.BOLD, 17));
		label_2.setBounds(10, 201, 76, 14);
		layeredPane.add(label_2);

		label_3 = new JLabel(new ImageIcon ("images\\gold.png"));
		label_3.setOpaque(false);
		label_3.setBounds(116, 9, 16, 16);
		layeredPane.add(label_3);

		label_4 = new JLabel(new ImageIcon ("images\\health.png"));
		label_4.setOpaque(false);
		label_4.setBounds(245, 9, 16, 16);
		layeredPane.add(label_4);



		for (int w = 0 ; w< getWidth(); w+=150 ){
			for (int h =0; h < getHeight(); h+=150){
				JLabel label = new JLabel(new ImageIcon("images\\menuBackground.png"));
				label.setBounds(w, h, 150, 150);
				layeredPane.add(label,-10);
			}
		}


		mouseListener= new MouseCastleMenu(this);
		okButton.addMouseListener(mouseListener);
		infantryLabel.addMouseListener(mouseListener);
		pikemanLabel.addMouseListener(mouseListener);
		cavalryLabel.addMouseListener(mouseListener);
	}

	private void drawUpgrades(){
		Upgrade[] upgrades = connector.getUpgrades(player);
		upgradesPanel.removeAll();

		if (upgrades != null){

			for (int i =0; i< upgrades.length; i++){

				JLabel nameLabel = new JLabel(upgrades[i].getName());
				nameLabel.setBounds(10, 38*i+9, 150, 20);
				nameLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
				nameLabel.setForeground(new Color(255, 215, 0));
				upgradesPanel.add(nameLabel);

				String path = "upgrades\\"+player.toLowerCase()+"\\"+upgrades[i].getImagePath();
				UpgradeLabel buyLabel = new UpgradeLabel(i, new ImageIcon(path),upgrades[i] );
				buyLabel.setToolTipText(upgrades[i].getDesc());
				buyLabel.addMouseListener(mouseListener);
				buyLabel.setBounds(130, 38*i+3, 32, 32);
				buyLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
				buyLabel.setForeground(new Color(255, 215, 0));
				upgradesPanel.add(buyLabel);

				JLabel priceLabel = new JLabel(String.valueOf(upgrades[i].getPrice()));
				priceLabel.setBounds(190, 38*i+9, 50, 20);
				priceLabel.setFont(new Font("Vijaya", Font.BOLD, 17));
				priceLabel.setForeground(new Color(255, 215, 0));
				upgradesPanel.add(priceLabel);
			}
			upgradesPanel.setPreferredSize(new Dimension (230,38* upgrades.length) );
		}
	}
}
